home *** CD-ROM | disk | FTP | other *** search
- /* Title: windcodes.c
- * Purpose: Allow searching of STD codes or towns
- * Author: Julyan Bristow
- * Date: August 1992 - February 1993
- */
- #include "wimp.h"
- #include "wimpt.h"
- #include "win.h"
- #include "event.h"
- #include "baricon.h"
- #include "res.h"
- #include "resspr.h"
- #include "menu.h"
- #include "template.h"
- #include "dbox.h"
- #include "werr.h"
- #include "bbc.h"
- #include "visdelay.h"
- #include "saveas.h"
- #include "xfersend.h"
- #include "xferrecv.h"
- #include "akbd.h"
- #include "coords.h"
-
- #include "stdio.h"
- #include "stdlib.h"
- #include "string.h"
- #include "ctype.h"
- /********************************* CONSTANTS ********************************/
- #define icon_m_i 1
- #define icon_m_srch 2
- #define icon_m_q 3
- #define srch_m_e 1
- #define srch_m_s 2
- #define save_m_a 1
- #define save_m_s 2
- #define edit_m_a 1
- #define edit_m_d 2
- #define Author_field 3
- #define Info_version 4
- #define Data_file "<STDFinder$Dir>.Directorys.STDCodes"
- #define Data_file_name "stdcodes"
- #define tft 0xfff
- #define fmnl 256
- #define mtnl 50
- #define mcl 8
- #define akbd_return 0x0d
- #define winmargin 8
- #define LINEHT 32
- /******************************** GLOBAL DATA *******************************/
- static char *Version = "2.00 (12 June 1993)";
- static char *Author = "Julyan Bristow";
- char *pathname;
-
- static menu iconbar_menu,search_menu,save_menu,edit_menu;
- static dbox search_box,edit_box,quit_box,load_box;
- static wimp_w wintext_handle; /* wimp_w being an abstract window handle */
- static wimp_w search_handle;
-
- static BOOL win_open = FALSE;
- static BOOL search_box_open = FALSE;
- static enum {search_entry = 1,srch_srch,srch_cancel,srch_clear,srch_sh_town,
- srch_sh_code,u_arrow,d_arrow,search_exact_option = 10 };
- static enum {e_town = 0,e_code,e_ok};
- static enum {q_dis = 4,q_save,q_can};
- static enum {l_over = 4,l_app,l_can};
-
- int maximum,all;
- static int found = 0;
- int modified = 0,edit = 0;
- int initialise = 1;
- int offset = 0;
- /***************** Define the structures for the towns and codes ****************/
- struct std {
- char town[mtnl];
- char code[mcl];
- struct std *next;
- struct std *previous;
- } *stdstart,*stdlast,*stdnode;
- struct search {
- char town[mtnl];
- char code[mcl];
- struct search *next;
- struct search *previous;
- } *srchstart,*srchlast,*s_node;
-
- /***************** Define the external routines ***************************/
- FILE *data_file;
- FILE *file_open(char *open_name,char *mode);
- void file_close(FILE *close_stream, char *close_name);
- int file_read(FILE *from_file,char *read_name,int action);
- int initial_file_read(FILE *from_file,char *read_name,int action);
- int string_search(char *search_for,int search_exact);
- char *string_to_lower(char *string);
-
- static BOOL export_as_text(char *filename,void *handle)
- {
- FILE *f;
- BOOL ok = TRUE;
- char ch;
- struct std *std_node;
- struct search *s_node;
- if (strlen(filename) > fmnl) {
- werr(0,"Filename is too long");
- return(FALSE);
- }
- f=fopen(filename,"w");
- if (f==0) { ok = FALSE; werr(0,"Cannot open file"); }
- else {
- maximum += 2;
- ch = ' ';
- if (all == 1) {
- std_node = stdstart;
- while (std_node) {
- fprintf(f,"%-*s%c%-6s\n",maximum,std_node->town,ch,std_node->code);
- std_node = std_node->next;
- }
- }
- if (all == 2) {
- std_node = stdstart;
- while (std_node) {
- fprintf(f,"%s,%s\n",std_node->town,std_node->code);
- std_node = std_node->next;
- }
- }
- if (all == 0) {
- s_node = srchstart;
- while (s_node) {
- fprintf(f,"%-*s%c%-6s\n",maximum,s_node->town,ch,s_node->code);
- s_node = s_node->next;
- }
- }
- }
- fclose(f);
- maximum -= 2; /* reset value of maximum */
- return(ok);
- }
- void window_tidy(char *window_name)
- {
- if (!strcmp(window_name,"search")) {
- search_box_open = TRUE;
- dbox_setfield(search_box,search_entry,"");
- }
- }
- static void save_menuproc(char *hit)
- {
- switch (hit[1])
- {
- case save_m_a:
- all = 1;
- saveas(tft,"<STDFinder$Dir>.All",0,export_as_text,0,0,0);
- break;
- case save_m_s:
- all = 0;
- saveas(tft,"<STDFinder$Dir>.Subset",0,export_as_text,0,0,0);
- break;
- }
- }
- static void edit_menuproc(char *hit)
- {
- switch (hit[1])
- {
- case edit_m_a:
- edit = 0; /* 0 = add */
- dbox_setfield(edit_box,e_town,"");
- dbox_setfield(edit_box,e_code,"");
- dbox_showstatic(edit_box);
- break;
- case edit_m_d:
- edit = 1; /* 1 = delete */
- dbox_setfield(edit_box,e_town,"");
- dbox_setfield(edit_box,e_code,"");
- dbox_showstatic(edit_box);
- break;
- }
- }
- static void search_menuproc(void *handle, char *hit)
- {
- handle = handle;
- switch (hit[0])
- {
- case srch_m_e:
- edit_menuproc(hit);
- break;
- case srch_m_s:
- save_menuproc(hit);
- break;
- }
- }
- /***************************** WINDOW FUNCTIONS *****************************/
- int win_lines(void)
- {
- wimp_wstate s;
- wimpt_noerr(wimp_get_wind_state(wintext_handle, &s));
- return (s.o.box.y1 - s.o.box.y0 - 2 * winmargin) / 32;
- }
- void wrele(FILE *out,int n)
- {
- int i;
- s_node = srchstart;
- if(!n) {
- fprintf(out,"%-*s%-8s\n",maximum,"Town","STD Code");
- return;
- }
- if (n > 1) {
- for (i=1;i<n;i++) {
- if(!s_node->next) return;
- else s_node = s_node->next;
- }
- }
- fprintf(out, "%-*s%-8s\n",maximum,s_node->town,s_node->code);
- }
- /*--- Create the window, yielding its handle. Return TRUE if ok. ---*/
- static BOOL create_window(char *name, wimp_w *handle)
- {
- int height;
- wimp_wind *window; /* Pointer of type window definition in this function */
-
- wimp_wstate boxstatus; /* variable of type wimp_state - contains info about position, size etc of window */
- wimp_openstr boxopen;
-
- wimpt_complain(wimp_get_wind_state(search_handle, &boxstatus));
- boxopen = boxstatus.o;
-
- window = template_syshandle(name);
- if (window == 0) return FALSE;
- window->title.indirecttext.buffer = "Search Results"; /* create the title of the window */
-
- /* if (found > 20) height = 20;*/
- height = found;
- window->box.x0 = boxopen.box.x0;
- window->box.x1 = boxopen.box.x0 + 16 * (maximum + mcl + 2);
- window->box.y1 = boxopen.box.y0 - (8 * winmargin);
- window->ex.y0 = -(((found + 1) * LINEHT) + winmargin);
- window->ex.y1 = 0;
- window->ex.x0 = 0;
- window->ex.x1 = (maximum + strlen("STD Codes")) * 16;
-
- return (wimpt_complain(wimp_create_wind(window, handle)) == 0);
-
- }
- static void wintext_open_window(wimp_openstr *o)
- {
- /* Just pass the open request on to the wimp - called from the wimp_EOPEN event handler */
- wimpt_noerr(wimp_open_wind(o));
- }
-
- static void wintext_event_handler(wimp_eventstr *e, /*void *handle*/ void *lf)
- {
- int more,bx,by,tl,bl;
- int win_lines(void);
- /* handle = handle; *//* We don't need handle (not sure why!): this stops compiler warning */
- wimp_redrawstr *r; /* pointer to a structure containing window area etc */
-
- switch (e->e) /* the first e is of type wimp_eventstr, which contains a structure of type wimp_etype e and of type wimpevent_data data */
- {
- case wimp_EREDRAW:
- /* wintext_redraw_window(e->data.o.w); */
-
- r = (wimp_redrawstr*)(&(e->data.o));
- wimpt_noerr(wimp_redraw_wind(r, &more));
- bx = r->box.x0 - r->scx;
- by = r->box.y1 - r->scy;
- while (more) /* Do the redraw loop */
- {
- tl = (by - r->g.y1) / LINEHT;
- if (tl < 0) tl = 0;
- bl = (by - r->g.y0) / LINEHT;
- if (bl > found) bl = found;
- for(;tl <= bl; tl++) {
- bbc_move(bx + winmargin, by - winmargin - (LINEHT * tl));
- ((void(*)(FILE*,int))lf)(stdout,tl);
- }
- wimp_get_rectangle(r, &more); /* redraw next rectangle until &more now reads FALSE */
- }
- break;
- case wimp_EOPEN:
- wintext_open_window(&e->data.o);
- win_open = TRUE;
- break;
- case wimp_ECLOSE: /* Pass on close request */
- wimpt_noerr(wimp_close_wind(e->data.o.w));
- win_open = FALSE;
- break;
- case wimp_ESCROLL:
- {
- int dy = 0;
- switch(e->data.scroll.y)
- {
- case 1:
- dy = 1; break;
- case -1:
- dy = -1; break;
- case 2:
- dy = win_lines(); break;
- case -2:
- dy = -win_lines(); break;
- }
- e->data.scroll.o.y += dy * 32;
- (void) wimpt_complain(wimp_open_wind(&e->data.scroll.o));
- }
- }
- }
- /************************* DIALOGUE BOX EVENT HANDLERS *************************/
- static void stdfinder_iconclick(wimp_i icon)
- {
- if (search_box_open == FALSE) {
- window_tidy("search");
- dbox_showstatic(search_box);
- dbox_setfield(search_box,search_entry,"");
-
- menu_setflags(save_menu,save_m_s,0,1);
- found = 0;
- }
- }
- static void stdcodes_info(void)
- {
- dbox d_info;
- if (d_info = dbox_new("ProgInfo"), d_info != NULL)
- {
- dbox_setfield(d_info, Info_version, Version);
- dbox_setfield(d_info, Author_field,Author);
- dbox_show(d_info);
- dbox_fillin(d_info);
- dbox_dispose(&d_info);
- }
- }
- static BOOL quit_event_handler(dbox edit_box,void *ev,void *handle)
- {
- wimp_eventstr *e=(wimp_eventstr*) ev;
- dbox_field field;
- handle = handle;
- switch (e->e)
- {
- case wimp_EOPEN:
- wimpt_noerr(wimp_open_wind(&e->data.o));
- break;
- case wimp_ECLOSE:
- dbox_hide(quit_box);
- return(TRUE);
- break;
- case wimp_EBUT:
- field = e->data.but.m.i;
- switch (field)
- {case q_dis:
- exit(0);
- break;
- case q_save:
- all = 2;
- saveas(tft,Data_file,0,export_as_text,0,0,0);
- exit(0);
- break;
- case q_can:
- dbox_hide(quit_box);
- break;
- default:
- break;
- }
- }
- return (TRUE);
- }
- static BOOL load_event_handler(dbox load_box,void *ev,void *handle)
- {
- wimp_eventstr *e=(wimp_eventstr*) ev;
- int temp_max;
- dbox_field field;
- handle = handle;
- switch (e->e)
- {
- case wimp_EOPEN:
- wimpt_noerr(wimp_open_wind(&e->data.o));
- break;
- case wimp_ECLOSE:
- dbox_hide(load_box);
- return(TRUE);
- break;
- case wimp_EBUT:
- field = e->data.but.m.i;
- switch (field)
- {case l_over:
- data_file = file_open(pathname,"r");
- visdelay_begin();
- temp_max = file_read(data_file,pathname,1);
- if(temp_max > maximum) maximum = temp_max;
- visdelay_end();
- file_close(data_file,pathname);
- dbox_hide(load_box);
- if (win_open == TRUE) {
- wimpt_noerr(wimp_close_wind(wintext_handle));
- win_open = FALSE;
- }
- break;
- case l_app:
- data_file = file_open(pathname,"r");
- visdelay_begin();
- temp_max = file_read(data_file,pathname,0);
- if(temp_max > maximum) maximum = temp_max;
- visdelay_end();
- file_close(data_file,pathname);
- dbox_hide(load_box);
- if (win_open == TRUE) {
- wimpt_noerr(wimp_close_wind(wintext_handle));
- win_open = FALSE;
- }
- break;
- case l_can:
- dbox_hide(load_box);
- break;
- default:
- break;
- }
- }
- return (TRUE);
- }
- static BOOL edit_event_handler(dbox load_box,void *ev,void *handle)
- {
- char town[mtnl],code[mcl];
- void add_entry(char *town,char *code);
- void delete_entry(char *town,char *code);
- wimp_eventstr *e=(wimp_eventstr*) ev;
- dbox_field field;
- handle = handle;
- switch (e->e)
- {
- case wimp_EOPEN:
- wimpt_noerr(wimp_open_wind(&e->data.o));
- break;
- case wimp_ECLOSE:
- dbox_hide(edit_box);
- return(TRUE);
- break;
- case wimp_EBUT:
- field = e->data.but.m.i;
- switch (field)
- {case e_ok:
- dbox_getfield(edit_box,e_town,town,mtnl-1);
- dbox_getfield(edit_box,e_code,code,mcl-1);
- if (!strlen(town) || !strlen(code)) {
- werr(0,"Incomplete data entry. Please try again.");
- break;
- }
- if (edit == 0) {
- add_entry(town,code);
- dbox_hide(edit_box);
- dbox_setfield(search_box,search_entry,"");
- break;
- }
- else {
- delete_entry(town,code);
- dbox_hide(edit_box);
- dbox_setfield(search_box,srch_sh_code,"");
- break;
- }
- default:
- break;
- }
- }
- return (TRUE);
- }
- int start_search(int exact)
- {
- char search_string[mtnl];
- if (win_open == TRUE) { wimpt_noerr(wimp_close_wind(wintext_handle)); win_open = FALSE; }
- dbox_getfield(search_box,search_entry,search_string,79);
- found = string_search(search_string,exact);
- if (found) {
- wimp_wstate winstatus; /* variable of type wimp_state - contains info about position, size etc of window */
- wimp_openstr wopen;
-
- s_node = srchstart;
- menu_setflags(save_menu, save_m_s, 0, 0);
-
- /* Create the main window, and declare its event handler */
- if (!create_window("Main", &wintext_handle)) return FALSE; /* Window creation failed */
- win_register_event_handler(wintext_handle, wintext_event_handler, (void*)wrele);
- event_attachmenu(wintext_handle,search_menu,search_menuproc,0);
-
- /* Get the state of the window - pass window's handle (global) and the determined status */
- wimpt_complain(wimp_get_wind_state(wintext_handle, &winstatus));
- wopen = winstatus.o;
-
- wopen.behind = -1; /* Make sure window is opened in front using this bit from the structure */
-
- wimpt_noerr(wimp_open_wind(&wopen)); /* in this line could have also used winstatus.o as these are the same */
- win_open = TRUE;
- offset = 0;
- wintext_open_window(&wopen);
-
- return found;
- }
- else {
- bbc_vdu(7);
- menu_setflags(save_menu, save_m_s, 0, 1);
- return NULL;
- win_open = FALSE;
- }
- }
- static BOOL search_event_handler(dbox search_box,void *ev,void *handle)
- {
- static int exact = 0;
- char search_string[mtnl];
- wimp_eventstr *e=(wimp_eventstr*) ev;
- dbox_field field;
- handle = handle;
- switch (e->e)
- {case wimp_ECLOSE:
- dbox_hide(search_box);
- dbox_hide(edit_box);
- search_box_open = FALSE;
- if (win_open) {
- wimpt_noerr(wimp_close_wind(wintext_handle));
- win_open = FALSE;
- }
- return(TRUE);
- break;
- case wimp_EKEY:
- switch (e->data.key.chcode) {
- case (akbd_return) :
- found = start_search(exact);
- s_node = srchstart;
- break;
- default:
- break;
- }
- break;
- case wimp_EBUT:
- field = e->data.but.m.i;
- switch (field)
- {case srch_srch:
- found = start_search(exact);
- s_node = srchstart;
- break;
- case srch_clear:
- strcpy(search_string,"");
- dbox_setfield(search_box,search_entry,search_string);
- menu_setflags(save_menu,save_m_s,0,1);
- if (win_open) {
- wimpt_noerr(wimp_close_wind(wintext_handle));
- win_open = FALSE;
- }
- found = 0;
- break;
- case srch_cancel:
- dbox_hide(search_box);
- dbox_hide(edit_box);
- search_box_open = FALSE;
- menu_setflags(save_menu,save_m_s,0,1);
- if (win_open) {
- wimpt_noerr(wimp_close_wind(wintext_handle));
- win_open = FALSE;
- }
- found = 0;
- break;
- case search_exact_option:
- exact = dbox_getnumeric(search_box,search_exact_option);
- dbox_setnumeric(search_box,search_exact_option,exact);
- break;
- default:
- break;
- }
- default:
- break;
- }
- return(FALSE);
- }
-
- static void iconbar_menuproc(void *handle, char *hit)
- {
- handle = handle;
- switch (hit[0])
- {
- case icon_m_i:
- stdcodes_info();
- break;
- case icon_m_srch:
- window_tidy("search");
- dbox_showstatic(search_box);
- dbox_setfield(search_box,search_entry,"");
- found = 0;
- menu_setflags(save_menu,save_m_s,0,1);
- break;
-
- case icon_m_q:
- if (modified) dbox_showstatic(quit_box);
- else exit(0);
- break;
- }
- }
- BOOL load_file(char *pathname)
- {
- /* Check that are not loading the "master" file */
- if (strstr(string_to_lower(pathname),Data_file_name)) if(!initialise) {
- werr(0,"Loading STDcodes again will not be carried out.");
- return TRUE;
- }
- if(initialise) {
- stdstart = stdlast = NULL;
- data_file = file_open(pathname,"r");
- maximum = initial_file_read(data_file,pathname,initialise);
- /* werr(0,"maximum = %d",maximum); */
- file_close(data_file,pathname);
- return TRUE;
- }
- dbox_show(load_box);
- return TRUE;
- }
- void iconbar_handler(wimp_eventstr *e, void *handle)
- {
- int filetype;
- e = e; /* prevent warnings */
- handle = handle;
- if ((filetype = xferrecv_checkinsert(&pathname)) != -1)
- if (load_file(pathname))
- xferrecv_insertfileok();
- }
- static BOOL make_menus()
- {
- if (iconbar_menu = menu_new("STD Codes",">Info,Search,Quit"), iconbar_menu == NULL)
- return FALSE;
- if (search_menu = menu_new("Results","Edit,Save"), search_menu == NULL)
- return FALSE;
- if (save_menu = menu_new("Save",">All,>~Subset"), save_menu == NULL)
- return FALSE;
- if (edit_menu = menu_new("Edit","Add,Delete"), edit_menu == NULL)
- return FALSE;
- menu_submenu(search_menu,1,edit_menu);
- menu_submenu(search_menu,2,save_menu);
- return TRUE;
- }
- /****************************** INITIALISATION ******************************/
- static BOOL std_initialise(void)
- {
- wimpt_init("Std Finder");
- res_init("stdfinder");
- resspr_init();
- template_init();
- dbox_init();
-
- search_box = dbox_new("Search");
- dbox_raw_eventhandler(search_box,search_event_handler,0);
-
- edit_box = dbox_new("Edit");
- dbox_raw_eventhandler(edit_box,edit_event_handler,0);
-
- load_box = dbox_new("Load");
- dbox_raw_eventhandler(load_box,load_event_handler,0);
-
- quit_box = dbox_new("Quit");
- dbox_raw_eventhandler(quit_box,quit_event_handler,0);
-
- if (!make_menus()) return FALSE;
-
- baricon("!stdfinder", (int)resspr_area(), stdfinder_iconclick);
- if (!event_attachmenu(win_ICONBAR,iconbar_menu,iconbar_menuproc,0))
- return FALSE;
-
- search_handle = dbox_syshandle(search_box);
- /* if (!event_attachmenu(search_handle,search_menu,search_menuproc,0))
- return FALSE; */
-
- win_register_event_handler(win_ICONBARLOAD, iconbar_handler, 0);
-
- initialise = 1;
- visdelay_begin();
- if (!load_file(Data_file)) return FALSE; /* allow first time */
- visdelay_end();
- srchstart = srchlast = NULL;
- initialise = 0;
-
- return TRUE;
- }
-
- /******************************* MAIN PROGRAM ********************************/
-
- /*--- Main entry point. ---*/
- int main()
- {
- if (std_initialise())
- {
- while (TRUE)
- event_process();
- }
-
- return 0;
- }
-